<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>浅拷贝</title>
</head>

<body>
  <script>
    // ============================== 浅拷贝方法 ==============================
    // 1. 对象拷贝
    const obj = {
      name: '佩奇'
    }
    // ============ Object.assign() ============
    const newObj = {}
    Object.assign(newObj,obj)
    console.log(newObj)
    newObj.name = '小猪'
    console.log(newObj)
    

    // ============ 展开运算符 ============
    const newObj = {
      ...obj
    }
    console.log(newObj)
    newObj.age = 18
    console.log(newObj)
    console.log(obj)
    
        
    // 2. 数组拷贝
    const arr = ['佩奇', '乔治']

    // ============ concat 方法 ============
    const newArr = arr.concat()
    newArr[0] = '哈哈'
    console.log(newArr)
    console.log(arr)

    // ============ 展开运算符 ============
    const arr2 = [...arr]
    console.log(arr2)
    
    // 3. 浅拷贝的问题： 如果遇到多层对象还是会影响原来的对象
    const pig = {
      name: '佩奇',  //基本数据类型可以拷贝
      family: {      //里层对象，该对象是没有拷贝的
        father: '猪爸爸'
      }
    }
    const pig2={...pig}
    pig2.family.father = '猪头'
    console.log(pig2)
    console.log(pig)  //father 也变成了猪头
  </script>
</body>

</html>